package com.ibeeking.found.file.rest.controller.v1;

import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.constants.GlobalConstant;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.common.enums.LoginRoleEnum;
import com.ibeeking.found.file.rest.service.ISourceGroupService;
import com.ibeeking.found.file.service.common.param.SourceGroupParam;
import com.ibeeking.found.file.service.common.query.SourceGroupQuery;
import com.ibeeking.found.file.service.common.vo.SourceGroupListVO;
import com.ibeeking.found.file.service.common.vo.SourceGroupVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName GoodsSourceGroupController控制器
 * @Description
 * @Author ibeeking
 * @Date 2021-02-23 15:10
 **/
@RestController
@RequestMapping("/sourcegroup/v1")
public class SourceGroupController {

    @Resource
    private ISourceGroupService goodsSourceGroupServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "素材分组列表")
    @PostMapping(value = "/list")
    public List<SourceGroupListVO> list(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Update.class) SourceGroupQuery sourceGroupQuery) {
        if(LoginRoleEnum.SHOP.name().equals(currentUser.getLoginSystem())){
            sourceGroupQuery.setShopId(currentUser.getCurrentShop().getShopId());
            sourceGroupQuery.setMerchantsBusiness(1);
        }else {
            sourceGroupQuery.setMerchantsBusiness(2);
        }
        return goodsSourceGroupServiceImpl.list(sourceGroupQuery);
    }

        @Log(logType = OperateLogTypeEnum.SAVE, describe = "素材分组添加")
    @PostMapping(value = "/add")
    public Boolean add(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Insert.class) SourceGroupParam sourceGroupParam) {
        if(LoginRoleEnum.SHOP.name().equals(currentUser.getLoginSystem())){
            sourceGroupParam.setShopId(currentUser.getCurrentShop().getShopId());
            sourceGroupParam.setMerchantsBusiness(1);
        }else {
            sourceGroupParam.setShopId(GlobalConstant.SHOP_ID_ZERO);
            sourceGroupParam.setMerchantsBusiness(2);
        }
        sourceGroupParam.setModifyBy(currentUser.getId());
        sourceGroupParam.setCreateBy(currentUser.getId());
        return goodsSourceGroupServiceImpl.add(sourceGroupParam);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "素材分组修改")
    @PostMapping(value = "/modify")
    public Boolean modify(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Update.class) SourceGroupParam sourceGroupParam) {
        sourceGroupParam.setModifyBy(currentUser.getId());
        return goodsSourceGroupServiceImpl.modify(sourceGroupParam);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "素材分组详情")
    @GetMapping(value = "/view/{id}")
    public SourceGroupVO view(@PathVariable("id") Long id) {
        return goodsSourceGroupServiceImpl.view(id);
    }


    @Log(logType = OperateLogTypeEnum.DEL, describe = "素材分组删除")
    @GetMapping(value = "/del/{id}")
    public Boolean del(@PathVariable("id") Long id) {
        return goodsSourceGroupServiceImpl.del(id);
    }
}